
# -*- coding:utf-8 -*-

# /*拼多多评价信息*/

import requests,json,time,random,os
from common.config import *
from common.Helper import Helper
from threading import Thread


class PddComments( object ):

    def __init__(self):

        pass


    def obtainTask(self):

        task = Helper.redisCase()

        print( task )

        self.requestIndex( task )


    # 页面请求
    def requestIndex(self,kwargs):

            # page页码
        page = 1

        while True:

            data = {"isLucky":-1,"afterSaleType":"5","pageNumber":"{0}".format( page ),"remarkStatus":-1,"pageSize":30,"source":"MMS","groupStartTime":kwargs['start_time'],"groupEndTime":kwargs['end_time']}

            r = requests.post(url=domain_url['orders_url'],headers=header,data=json.dumps(data) )


            # print(r.status_code == 200 and r.json()['result'] == 'null')
            # 判断cookie是否过期
            if r.status_code == 200 and '会话已过期' in json.dumps(r.json(),ensure_ascii=False):

                print('COOKIE已经过期')
                #回传COOKIE失败接口
                # 返回任务失败

                return False

            if r.status_code == 200 and '操作过于频繁，请稍后再试' in json.dumps(r.json(), ensure_ascii=False):


                print('操作频繁,进行随机睡眠.....')

                time.sleep( random.randint(20,30) )

                return False

            elif r.status_code == 200 and len(r.json()['result']['pageItems']) != 0 :

                items = r.json()['result']['pageItems']

                self.parseIndex ( items  )


            elif r.status_code == 200 and len(r.json()['result']['pageItems']) == 0 :

                print('该时间段无订单信息............')

                return



    # 解析信息
    def parseIndex(self,items):

        for item in items:

            order_sn = item['order_sn']

            data = {'order_sn':order_sn}

            Helper.saveReids( data )

    # api上传
    def apiRequest(self):

        pass

    def main(self):


        while True:


            self.obtainTask()

            time.sleep(2)


if __name__ == '__main__':

    c = PddComments()

    # c.main()

    threads = []

    for i in range(3):

        t = Thread( target=c.main,args=())

        t.start()

        threads.append(t)

    for t in threads:

        t.join()


    # c.obtainTask()